﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Entities;
using System.Data.SqlClient;

namespace DataAccess
{
    public class SeatesDAL : BaseObject
    {
        #region Public Methods
        ///<summary>
        ///Create Obj from ireader.
        ///</summary>

        private Seates GetObjectFromReader(IDataReader reader)
        {
            Seates temp = new Seates();
            temp.SeatNo = (reader["SeatNo"] is DBNull) ? int.MinValue : (System.Int32)reader["SeatNo"];
            temp.CoachNo = (reader["CoachNo"] is DBNull) ? int.MinValue : (System.Int32)reader["CoachNo"];
            return temp;
        }

        ///<summary>
        ///Get List Seates and store in a list 
        ///</summary>
        public Seates Get_Seates_By_ID(int SeatNo)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@SeatNo", SeatNo));
                conn.Open();
                Seates obj = new Seates();
          
                using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                    {
                        obj =  GetObjectFromReader(reader);
                    }
                }
                conn.Close();
                return obj;
            }
        }

        ///<summary>li
        ///Get List Seates as DataTable 
        ///</summary>
        /// <param name="whereSql">Where fields. example : 1=1 </param>
        /// <param name="orderBy"Order fields></param>
        public List<Seates> GetList_Seates(string whereSql, string orderBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@WhereCondition", whereSql));
                cmd.Parameters.Add(new SqlParameter("@OrderByExpression", orderBy));
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    List<Seates> lst = new List<Seates>();

                    using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            lst.Add(GetObjectFromReader(reader));
                        }
                    }
                    conn.Close();
                    return lst;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>li
        ///Get All Seates as DataTable 
        ///</summary>
        /// <param name="whereSql">Where fields. example : 1=1 </param>
        /// <param name="orderBy"Order fields></param>
        public List<Seates> GetAll_Seates()
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    List<Seates> lst = new List<Seates>();

                    using (IDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (reader.Read())
                        {
                            lst.Add(GetObjectFromReader(reader));
                        }
                    }
                    conn.Close();
                    return lst;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Using for paging data 
        ///</summary>
        /// <param name="pageIndex">Number of pages</param>
        /// <param name="PageSize">Number of a rows per page</param>
        /// <param name="conditions">Where  fields</param>
        /// <param name="groupBy">Group by fields</param>
        public DataTable Get_SeatesAsPaging(int pageIndex, int pageSize, string sSortColumn, string sFields, string sWhere, string sGroupBy)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
                cmd.Parameters.Add(new SqlParameter("@PageSize", pageSize));
                cmd.Parameters.Add(new SqlParameter("@SortColumn", sSortColumn));
                cmd.Parameters.Add(new SqlParameter("@Fields", sFields));
                cmd.Parameters.Add(new SqlParameter("@Where", sWhere));
                cmd.Parameters.Add(new SqlParameter("@Group", sGroupBy));
                SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
                try
                {
                    conn.Open();
                    DataTable tbl = new DataTable();
                    dataAdapter.Fill(tbl);
                    conn.Close();
                    return tbl;
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Insert new Seates
        ///</summary>
        public void Insert_Seates(Seates temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@SeatNo", temp.SeatNo));
                cmd.Parameters.Add(new SqlParameter("@CoachNo", temp.CoachNo));

                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Update a Seates
        ///</summary>
        public void Update_Seates(Seates temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.Parameters.Add(new SqlParameter("@SeatNo", temp.SeatNo));
                cmd.Parameters.Add(new SqlParameter("@CoachNo", temp.CoachNo));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        ///<summary>
        ///Delete a Seates
        ///</summary>
        public void Delete_Seates(Seates temp)
        {
            using (SqlConnection conn = CreateConnection())
            {
                SqlCommand cmd = new SqlCommand("", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@SeatNo", temp.SeatNo));
                try
                {
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch (SqlException ex)
                {
                    throw ex;
                }
                finally
                {
                    if (conn.State == ConnectionState.Open)
                        conn.Close();
                }
            }
        }

        #endregion

    }
}
